<?php
/**
 * Klasa student
 * @author Miron Pawlik
 */

class Student
{
	private $index;
	private $name;
	private $surname;
	private $year;
	private $admin;
	private $register;
	private $locked;
	private $subjectsChoose;
	/**
	 * Tworzenie nowego obiektu
	 *
	 * @param integer $indexNr indtyfikator uzytkownika
	 */
	public function __construct(dbHandler $db, $indexNr)
	{
		if($data = $db->getById($indexNr, 'students', '*', 'indexID'))
		{
			$this->index = $data['indexID'];
			$this->name = $data['name'];
			$this->surname = $data['surname'];
			$this->year = $data['year'];
			$this->admin = $data['admin'];
			$this->register = $data['register'];
			$this->locked = $data['locked'];
			$this->subjectsChoose = $data['subjectsChoose'];
			
		}
		else
			throw new Exception("Brak użytkownika o podanym numerze");
	}
	/**
	 * Loguje użytkownika i zwraca jego identyfikator lub 0 w przypadku niepowodzenia
	 *
	 * @param dbHandler $db
	 * @param int $indexNr numer indeksu
	 * @param string $password haslo usera
	 * @return int
	 */
	public static function login(dbHandler $db, $indexNr, $password)
	{
		$data = $db->getById($indexNr, 'students', '*', 'indexID');
		if($data['password'] == md5($password))
			return $indexNr;
		else
			return 0;
	}
	/**
	 * Sprawdza czy aktualny user ma prawa administratora
	 *
	 * @return bool
	 */
	public function isAdmin()
	{
		return (bool)$this->admin;
	}
	
	/**
	 * Zwraca numer indeksu aktualnego studenta
	 *
	 * @return bool
	 */
	public function getId()
	{
		return $this->index;
	}
	
	/**
	 * Zwraca imię i nazwisko
	 *
	 * @return string
	 */
	public function getName()
	{
		return $this->name." ".$this->surname;
	}
	
	/**
	 * Zwraca rocznik
	 *
	 * @return int
	 */
	public function getYear()
	{
		return $this->year;	
	}
	
	public function isRegister()
	{
		return $this->register;
	}
	
	public function isLocked()
	{
		return $this->locked;
	}
	
	public function hasChooseSubjects()
	{
		return $this->subjectsChoose;
	}
	
	public function __toString()
	{
		return $this->getName();
	}
	
	/**
	 * Uaktualnie informacje o danym studencie
	 *
	 * @param dbHandler $db
	 * @param int $index
	 * @param string $name
	 * @param string $surname
	 * @param ing $year
	 * @param string $password
	 * @param bool $admin
	 */
	public function updateUser(dbHandler $db, $index, $name, $surname, $year, $password = false, $locked = 0, $admin = 0)
	{
		if($this->isAdmin())
		{
			$a = array('name' => $name, 'surname' => $surname, 'year' => $year);
			if($password)
				$a['password'] = md5($password);
			
			if($admin || $index == $this->index)
				$a['admin'] = 1;
			else
				$a['admin'] = 0;
			
			if($locked)
				$a['locked'] = 1;
			else
				$a['locked'] = 0;
			$db->editById($index, 'students', $a, 'indexID');
		}
		else
			throw new Exception("Brak uprawnień");
	}
	
	/**
	 * Dodaje nowego uzytkownika do bazy
	 *
	 * @param dbHandler $db
	 * @param int $index
	 * @param string $name
	 * @param string $surname
	 * @param ing $year
	 * @param string $password
	 * @param bool $admin
	 */
	public function addUser(dbHandler $db, $index, $name, $surname, $year, $password, $admin = 0)
	{
		if($this->isAdmin())
		{
			$a = array('indexID' => $index, 'name' => $name, 'surname' => $surname, 'year' => $year);
			if($password)
				$a['password'] = md5($password);
			if($admin)
				$a['admin'] = 1;
			$db->addNew('students', $a);
		}
		else
			throw new Exception("Brak uprawnień");
	}
}

